(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('apEffectiveSettingService', apEffectiveSettingService);
	apEffectiveSettingService.$inject = [ '$q', '$timeout', 'loggerFactory', 'ajaxServiceFactory', 'modalService', 'gridService', 'arithService', 'profFilterOptionService' ];
	function apEffectiveSettingService($q, $timeout, loggerFactory, ajaxServiceFactory, modalService, gridService, arithService, profFilterOptionService) {
		var log = loggerFactory.getLogger("apEffectiveSettingService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getApEffectiveSettingOptions : {
				url : 'do/cda/dataList/apEffectiveSettingOptions',
				method : 'get'
			},
		});
		function getApEffectiveSettingOptions() {
			var defer = $q.defer();
			ajaxService.getApEffectiveSettingOptions().then(function(response) {
				var apEffectiveSettingOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						apEffectiveSettingOptions[key] = tempValue;
					});
				}
				defer.resolve(apEffectiveSettingOptions);
			});
			return defer.promise;
		}
		
		function initGeneral($scope, ctrl, updateProcessFlag) {
			var defer = $q.defer();
			profFilterOptionService.initProfFilterOptions(ctrl).then(function() {
				getApEffectiveSettingOptions().then(function(response) {
					ctrl.yesNoOptions = response.yesNoOptions;
					ctrl.prodCodeOptions = response.allProdCodeOptions;
					// for ProfSetlinst
					ctrl.txnCcyOptions = angular.copy(response.currencyByFxRateOptions);
					ctrl.setlCcyOptions = angular.copy(response.currencyByFxRateOptions);
					ctrl.crDrOptions = response.crDrOptions;
					ctrl.allPaycodeOptions = response.allPaycodeOptions;
					ctrl.itemTypeOptions = response.itemTypeOptions;
					ctrl.intCatOptions = response.intCatOptions;// multi interest
					defer.resolve();
				}, angular.noop);
			});
			
			
			return defer.promise;
		}
		
		function initLayoutConfigAndModel ($scope, ctrl, initApEffectiveSetting, initLayoutConfig, updateProcessFlag) {
			var newLayoutConfig = addLayoutConfigForMulti(ctrl, initLayoutConfig);
			initMultiTableDefine(newLayoutConfig, $scope, ctrl, updateProcessFlag);
			ctrl.layoutConfig = newLayoutConfig;
			$timeout(function() {
				// reset model after change layout
				resetDataModel(ctrl, initApEffectiveSetting);
				ctrl.apEffectiveSetting = initApEffectiveSetting;
			});
		}
		
		function addLayoutConfigForMulti(ctrl, resLayoutConfig) {
			if (!resLayoutConfig) return resLayoutConfig;
			var profIntLayoutTemplate = resLayoutConfig.formConfigs.profInt;
			if (ctrl.intCatOptions && ctrl.intCatOptions.length > 0) {
				for (var i = 0; i < ctrl.intCatOptions.length; i++) {
					var intCat = ctrl.intCatOptions[i].value;
					var layoutTmp = angular.copy(profIntLayoutTemplate);
					layoutTmp.name = 'profInt' + intCat;
					if (layoutTmp.tableConfigs.table.viewRow)
						layoutTmp.tableConfigs.table.viewRow = layoutTmp.tableConfigs.table.viewRow + '.' + intCat;
					resLayoutConfig.formConfigs['profInt' + intCat] = layoutTmp;
				}
			}
			return resLayoutConfig;
		}
		
		function initMultiTableDefine(newLayoutConfig, $scope, ctrl, updateProcessFlag) {
			initMultiProfInt(newLayoutConfig, $scope, ctrl, updateProcessFlag);
		}
		
		/**
		 * build apEffectiveSettingTmp profInts
		 */
		function resetDataModel(ctrl, initApEffectiveSetting) {
			var initApEffectiveSettingTmp = {};
			if (ctrl.intCatOptions && ctrl.intCatOptions.length > 0) {
				for (var i = 0; i < ctrl.intCatOptions.length; i++) {
					var intCat = ctrl.intCatOptions[i].value;
					var belongIntCatProfInts = [];
					if (initApEffectiveSetting.profInts && initApEffectiveSetting.profInts.length > 0) {
						for (var j = 0; j < initApEffectiveSetting.profInts.length; j++) {
							if (initApEffectiveSetting.profInts[j].intCat == intCat) {
								belongIntCatProfInts.push(initApEffectiveSetting.profInts[j]);
							}
						}
					}
					initApEffectiveSettingTmp['profInts' + intCat] = belongIntCatProfInts;
				}
			}
			ctrl.apEffectiveSettingTmp =  initApEffectiveSettingTmp;
		}
		
		function initMultiProfInt(initLayoutConfig, $scope, ctrl, updateProcessFlag) {
			if(ctrl.intCatOptions){
				ctrl.profIntCats = angular.copy(ctrl.intCatOptions);// for page ng-repeat
				ctrl.profIntMultiInnerGridOptions = {};
				ctrl.selectedProfInts = {};
				ctrl.viewProfInt = {};
				angular.forEach(ctrl.intCatOptions, function(intCatOption) {
					var intCat = intCatOption.value;
					
					//init profCostOfFundInt grid
					var profIntLayoutName = 'profInt' + intCat;
					var profIntRowSelectionChanged = {};
					profIntRowSelectionChanged[intCat] = function(eventRow, selectedRows) {
						ctrl.selectedProfInts[intCat] = selectedRows;
					}
					ctrl.profIntMultiInnerGridOptions[intCat] = gridService.createInnerGridOptions($scope, initLayoutConfig.formConfigs[profIntLayoutName].tableConfigs.table, profIntRowSelectionChanged[intCat]);
					ctrl.profIntMultiInnerGridOptions[intCat].data = 'vm.apEffectiveSettingTmp.profInts' + intCat;
					
					ctrl.viewProfInt[intCat] = function(currRow) {		
						var params = {};
						params.profInt = angular.copy(currRow.entity);
						params.prodSubtypeExtProperty = ctrl.apEffectiveSetting.prodSubtypeExtProperty;
						modalService.openCustomDialogs({
						   'title': 'Interest Info',
						   'dialogsModule' : 'cda.profint.popupview',
						   'size' : [1100,500],
						   'params':params,
						   'btnCallback' : {
								'ok' : function(profInt) {						
								}
							}		
						});
					};
					
				})
			}
		}
		
		
		function initProfPastdue($scope, ctrl, initLayoutConfig, updateProcessFlag) {
			//init profPastdue grid
			ctrl.profPastdueInnerGridOptions = gridService.createInnerGridOptions($scope, initLayoutConfig.formConfigs.profPastdue.tableConfigs.table, profPastdueRowSelectionChanged);
			ctrl.profPastdueInnerGridOptions.data = 'vm.apEffectiveSetting.profPastdues';
			function profPastdueRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedProfPastdues = selectedRows;
			}
			
			ctrl.viewProfPastdue = function(currRow) {		
				var params = {};
				params.profPastdue = angular.copy(currRow.entity);
				params.prodSubtypeExtProperty = ctrl.apEffectiveSetting.prodSubtypeExtProperty;
				modalService.openCustomDialogs({
				   'title': 'Past Due Info',
				   'dialogsModule' : 'cda.profpastdue.popupview',
				   'params':params,
				   'btnCallback' : {
						'ok' : function(profPastdue) {						
						}
					}		
				});
			};
		};
		
		
		function initProfFee($scope, ctrl, initLayoutConfig, updateProcessFlag) {
			//init profFee grid
			ctrl.profFeeInnerGridOptions = gridService.createInnerGridOptions($scope, initLayoutConfig.formConfigs.profFee.tableConfigs.table, profFeeRowSelectionChanged);
			ctrl.profFeeInnerGridOptions.data = 'vm.apEffectiveSetting.profFees';
			function profFeeRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedProfFees = selectedRows;
			}
			
			ctrl.viewProfFee = function(currRow) {		
				var params = {};
				params.profFee = angular.copy(currRow.entity);
				params.prodType = ctrl.apEffectiveSetting.prodSubtypeExtProperty.prodType;
				modalService.openCustomDialogs({
				   'title': 'Fee Info',
				   'dialogsModule' : 'cda.proffee.popupview',
				   'size' : [1100,500],
				   'params':params,
				   'btnCallback' : {
						'ok' : function(profFee) {						
						}
					}		
				});
			};
		};
		
		
		function initProfSetlinst($scope, ctrl, initLayoutConfig, updateProcessFlag) {
			//init buyerProfSetlinst grid
			ctrl.buyerProfSetlinstInnerGridOptions = gridService.createInnerGridOptions($scope, initLayoutConfig.formConfigs.profSetlinst.tableConfigs.table, buyerProfSetlinstRowSelectionChanged);
			ctrl.buyerProfSetlinstInnerGridOptions.data = 'vm.apEffectiveSetting.buyerProfSetlinsts';
			function buyerProfSetlinstRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedBuyerProfSetlinsts = selectedRows;
			}
			
			//init sellerProfSetlinst grid
			ctrl.sellerProfSetlinstInnerGridOptions = gridService.createInnerGridOptions($scope, initLayoutConfig.formConfigs.profSetlinst.tableConfigs.table, sellerProfSetlinstRowSelectionChanged);
			ctrl.sellerProfSetlinstInnerGridOptions.data = 'vm.apEffectiveSetting.sellerProfSetlinsts';
			function sellerProfSetlinstRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedSellerProfSetlinsts = selectedRows;
			}
		};
		
		return {
			initGeneral : initGeneral,
			initLayoutConfigAndModel : initLayoutConfigAndModel,
			initProfPastdue : initProfPastdue,
			initProfFee : initProfFee,
			initProfSetlinst : initProfSetlinst
		};
	}
})(angular);